/**
 * Created by Administrator on 2018/4/17 20:58:33.
 */
var dateSelector = {
    beginYear : 2000,
    getCurrentYearNum : function() {
        return new Date().getFullYear();
    },
    getEndWithCurrentAllYearArr : function() {
        var allYearArr = [];
        var currentYearNum = Number.parseInt(this.getCurrentYearNum());
        var index = 0;
        while(currentYearNum > this.beginYear){
            allYearArr[index++] = currentYearNum;
            currentYearNum--;
        }
        return allYearArr;
    },
    getAllMonthArr : function(){
        var allMonthArr = [];
        for(var i=1; i<=12; i++){
            allMonthArr[i-1] = i;
        }
        return allMonthArr;
    },
    getDayArrByYearAndMonth : function(yearNum, monthNum){
        var allDayArr = [];
        var endDayNum = new Date(yearNum, monthNum, 0).getDate();
        for(var i=1; i<=endDayNum; i++){
            allDayArr[i-1] = i;
        }
        return allDayArr;
    },
    initDayDate : function(_year, _month, _day){
        var allDayArr = this.getDayArrByYearAndMonth(_year.val(), _month.val());
        _day.empty();
        _day.append("<option value='0'>日</option>");
        for(var i=0; i<allDayArr.length; i++){
            _day.append("<option value='" + allDayArr[i] + "'>" + allDayArr[i] + "</option>")
        }
    },
    init : function(_year, _month, _day){
        var allYearNumArr = this.getEndWithCurrentAllYearArr();
        _year.empty();
        _year.append("<option value='0'>年</option>");
        for(var i=0; i<allYearNumArr.length; i++){
            _year.append("<option value='" + allYearNumArr[i] + "'>" + allYearNumArr[i] + "</option>")
        }

        var allMonthArr = this.getAllMonthArr();
        _month.empty();
        _month.append("<option value='0'>月</option>");
        for(var i=0; i<allMonthArr.length; i++){
            _month.append("<option value='" + allMonthArr[i] + "'>" + allMonthArr[i] + "</option>")
        }

        if(_day.val()){
            var dayVal = _day.val();
            dateSelector.initDayDate(_year, _month, _day)
            _day.val(dayVal);
        }

        $(_year).change(function(){
            dateSelector.initDayDate(_year, _month, _day)
        });
        $(_month).change(function(){
            dateSelector.initDayDate(_year, _month, _day)
        });
    }
};